package com.ccl.iot.monitor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.ccl.iot.publics.UIUtils;

public class HomeKeyMonitor extends Monitor {
    public interface OnHomeKeyDownListener extends MonitorListener {
        public void onHomeKeyDownChange(boolean aOn);
    }

    /*
     * Static functions
     */
    private static HomeKeyMonitor gDefault = null;

    public static void Initialize(Context aContext) {
        if (gDefault == null) {
            gDefault = new HomeKeyMonitor(aContext);
        }
    }

    public static boolean InstanceOf(Monitor aMonitor) {
        return aMonitor == gDefault;
    }

    public static boolean AddOnHomeKeyDownListener(OnHomeKeyDownListener aListener) {
        synchronized (HomeKeyMonitor.class) {
            if (gDefault == null) {
                Initialize(UIUtils.getContext());
            }
            if (gDefault != null) {
                return gDefault.addListener(aListener);
            }
        }

        return false;
    }

    public static boolean RemoveOnHomeKeyDownListener(OnHomeKeyDownListener aListener) {
        synchronized (HomeKeyMonitor.class) {
            if (gDefault != null) {
                return gDefault.removeListener(aListener);
            }
        }
        return false;
    }

    /*
     * 	Non-static functions
     */
    private Context iContext = null;

    private HomeKeyMonitor(Context aContext) {
        iContext = aContext;
    }

    private void onHomeKeyDown(boolean longPress) {
        int tIndex = 0;
        OnHomeKeyDownListener tListener;

        tListener = (OnHomeKeyDownListener) getFirstListener();
        while (tListener != null) {
            tListener.onHomeKeyDownChange(longPress);
            tListener = (OnHomeKeyDownListener) getNextListener(tIndex++);
        }
    }

    private BroadcastReceiver iHomeKeyDowmListener = new BroadcastReceiver() {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                        // 短按home键
                        onHomeKeyDown(false);
                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                        // 长按home键
                        onHomeKeyDown(true);
                    }
                }
            }
        }
    };

    @Override
    public boolean startMonitor() {
        if (iContext != null) {
            if (super.startMonitor()) {
                IntentFilter tFilter = new IntentFilter();

                tFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                iContext.registerReceiver(iHomeKeyDowmListener, tFilter);
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean stopMonitor(boolean aForce) {
        if (super.stopMonitor(aForce)) {
            if (iContext != null) {
                iContext.unregisterReceiver(iHomeKeyDowmListener);
            }

            return true;
        }

        return false;
    }
}
